//
//  DaltonizerTests.m
//  iSeeColor
//
//  Created by Wyatt Gosling on 10/26/12.
//  Copyright (c) 2012 Wyatt Gosling. All rights reserved.
//

#import "DaltonizerTest.h"

#define RED i+0
#define GREEN i+1
#define BLUE i+2
#define ALPHA i+3

@implementation DaltonizerTests
@synthesize dal;
@synthesize width;
@synthesize height;
@synthesize size;

- (void)setUp
{
    [super setUp];
    
    dal = [[Daltonizer alloc] init];
    width  = 10;
    height = 10;
    size   = width * height * 4;

    dal.imageSize = size;
}

- (void)tearDown
{
    [super tearDown];
}

- (void) test_alphaAlwaysMax
{
    unsigned char* orig = malloc(size);
    unsigned char* dest = malloc(size);

    for (int i = 0; i < size; i++) {
        orig[i] = i;
    }

    dest = [dal daltonizeBitmap:orig];

    for (int i = 0; i < size; i+=4) {
        STAssertEquals( dest[ALPHA], (unsigned char)255, @"Pixel %u did not have alpha value 255. Instead got %u.", i/4, dest[ALPHA] );
    }
}

- (void) test_daltonizeBlue
{
    unsigned char* orig = malloc(size);
    unsigned char* dest = malloc(size);
    for (int i = 0; i < size; i += 4) {
        orig[RED  ] = 0;
        orig[GREEN] = 0;
        orig[BLUE ] = 255;
        orig[ALPHA] = 255;
    }

    dest = [dal daltonizeBitmap:orig];

    for (int i = 0; i < size; i += 4) {
        STAssertEquals( dest[RED  ], orig[RED  ],         @"Pixel(r):%u got:%u want:%u", i/4, dest[RED  ], orig[RED  ] );
        STAssertEquals( dest[GREEN], orig[GREEN],         @"Pixel(g):%u got:%u want:%u", i/4, dest[GREEN], orig[GREEN] );
        STAssertEquals( dest[BLUE ], orig[BLUE ],         @"Pixel(b):%u got:%u want:%u", i/4, dest[BLUE ], orig[BLUE ] );
        STAssertEquals( dest[ALPHA], (unsigned char) 255, @"Pixel(a):%u got:%u want:%u", i/4, dest[ALPHA], orig[ALPHA] );
    }
}

- (void) test_daltonizeYellow
{
    unsigned char* orig = malloc(size);
    unsigned char* dest = malloc(size);
    for (int i = 0; i < size; i += 4) {
        orig[RED  ] = 255;
        orig[GREEN] = 255;
        orig[BLUE ] = 0;
        orig[ALPHA] = 255;
    }

    dest = [dal daltonizeBitmap:orig];

    for (int i = 0; i < size; i += 4) {
        STAssertEquals( dest[RED  ], orig[RED  ],         @"Pixel(r):%u got:%u want:%u", i/4, dest[RED  ], orig[RED  ] );
        STAssertEquals( dest[GREEN], orig[GREEN],         @"Pixel(g):%u got:%u want:%u", i/4, dest[GREEN], orig[GREEN] );
        STAssertEquals( dest[BLUE ], orig[BLUE ],         @"Pixel(b):%u got:%u want:%u", i/4, dest[BLUE ], orig[BLUE ] );
        STAssertEquals( dest[ALPHA], (unsigned char) 255, @"Pixel(a):%u got:%u want:%u", i/4, dest[ALPHA], orig[ALPHA] );
    }
}

- (void) test_daltonizeGreen
{
    unsigned char* orig = malloc(size);
    unsigned char* dest = malloc(size);
    for (int i = 0; i < size; i += 4) {
        orig[RED  ] = 0;
        orig[GREEN] = 255;
        orig[BLUE ] = 0;
        orig[ALPHA] = 255;
    }

    dest = [dal daltonizeBitmap:orig];

    for (int i = 0; i < size; i += 4) {
        STAssertEquals( dest[RED  ], (unsigned char) 226, @"Pixel(r):%u got:%u want:%u", i/4, dest[RED  ], 226);
        STAssertEquals( dest[GREEN], (unsigned char) 255, @"Pixel(g):%u got:%u want:%u", i/4, dest[GREEN], 255);
        STAssertEquals( dest[BLUE ], (unsigned char) 113, @"Pixel(b):%u got:%u want:%u", i/4, dest[BLUE ], 113);
        STAssertEquals( dest[ALPHA], (unsigned char) 255, @"Pixel(a):%u got:%u want:%u", i/4, dest[ALPHA], 255);
    }
}

- (void) test_daltonizeRed
{
    unsigned char* orig = malloc(size);
    unsigned char* dest = malloc(size);
    for (int i = 0; i < size; i += 4) {
        orig[RED  ] = 255;
        orig[GREEN] = 0;
        orig[BLUE ] = 0;
        orig[ALPHA] = 255;
    }

    dest = [dal daltonizeBitmap:orig];

    for (int i = 0; i < size; i += 4) {
        STAssertEquals( dest[RED  ], (unsigned char)  29, @"Pixel(r):%u got:%u want:%u", i/4, dest[RED  ],  29);
        STAssertEquals( dest[GREEN], (unsigned char) 170, @"Pixel(g):%u got:%u want:%u", i/4, dest[GREEN], 170);
        STAssertEquals( dest[BLUE ], (unsigned char) 115, @"Pixel(b):%u got:%u want:%u", i/4, dest[BLUE ], 115);
        STAssertEquals( dest[ALPHA], (unsigned char) 255, @"Pixel(a):%u got:%u want:%u", i/4, dest[ALPHA], 255);
    }
}

- (void) test_daltonizeWhite
{
    unsigned char* orig = malloc(size);
    unsigned char* dest = malloc(size);
    for (int i = 0; i < size; i += 4) {
        orig[RED  ] = 255;
        orig[GREEN] = 255;
        orig[BLUE ] = 255;
        orig[ALPHA] = 255;
    }

    dest = [dal daltonizeBitmap:orig];

    for (int i = 0; i < size; i += 4) {
        STAssertEquals( dest[RED  ], (unsigned char) 255, @"Pixel(r):%u got:%u want:%u", i/4, dest[RED  ], 255);
        STAssertEquals( dest[GREEN], (unsigned char) 255, @"Pixel(g):%u got:%u want:%u", i/4, dest[GREEN], 255);
        STAssertEquals( dest[BLUE ], (unsigned char) 255, @"Pixel(b):%u got:%u want:%u", i/4, dest[BLUE ], 255);
        STAssertEquals( dest[ALPHA], (unsigned char) 255, @"Pixel(a):%u got:%u want:%u", i/4, dest[ALPHA], 255);
    }
}

- (void) test_daltonizeBlack
{
    unsigned char* orig = malloc(size);
    unsigned char* dest = malloc(size);
    for (int i = 0; i < size; i += 4) {
        orig[RED  ] =   0;
        orig[GREEN] =   0;
        orig[BLUE ] =   0;
        orig[ALPHA] = 255;
    }

    dest = [dal daltonizeBitmap:orig];

    for (int i = 0; i < size; i += 4) {
        STAssertEquals( dest[RED  ], (unsigned char)   0, @"Pixel(r):%u got:%u want:%u", i/4, dest[RED  ],   0);
        STAssertEquals( dest[GREEN], (unsigned char)   0, @"Pixel(g):%u got:%u want:%u", i/4, dest[GREEN],   0);
        STAssertEquals( dest[BLUE ], (unsigned char)   0, @"Pixel(b):%u got:%u want:%u", i/4, dest[BLUE ],   0);
        STAssertEquals( dest[ALPHA], (unsigned char) 255, @"Pixel(a):%u got:%u want:%u", i/4, dest[ALPHA], 255);
    }
}
@end
